<template>
  <div class="progress-body" v-if="showUpdate">
    <div class="progress-extra">
      <Icon type="md-close" size="22" class="extra-close" @click="setShowUpdate(false)"/>
    </div>
    <div :style="{width:ballRadius*2+'px',height:ballRadius*2+'px'}" class="ball">
      <!-- SVG 圆形元素 -->
      <svg style="position: absolute" height="95%" viewBox="-160 -160 320 320" width="95%"
           xmlns="http://www.w3.org/2000/svg">
        <path id="svg"
              d="M 0 160
         A 160 160 0 0 1 0 -160
         A 160 160 0 0 1 0 160 Z"
              fill="transparent" stroke="none"/>
      </svg>
      <img alt="Logo" class="logo" src="/public/img/log-opacity.png">
      <div style="display: flex;flex-direction: column;width: 60%">
        <Progress :percent="update_info.percent || 0" :stroke-width="5" style="flex-grow: 1;margin-right: 4px">
          <div style="width: 100%;display: flex;flex-direction: row;justify-content: center;align-content: center">
            <span>{{ update_info.percent || 0 }}%</span>
            <span style="color: #1a1a1a;margin-left: 8px">{{ update_info.speed || '0kb' }}</span>
          </div>
        </Progress>
      </div>
      <div class="particles">
        <!--        循环渲染粒子-->
        <div v-for="(particleItem,index) in particlesList"
             :key="index"
             :ref="el => setItemRef(index, el)"
             :style="{left: particleItem.left+'px',top:particleItem.top+'px',backgroundColor:particleItem.color}"
             class="particle">
        </div>
      </div>
    </div>
    <div class="progress-action">
      <Button type="primary" @click="startUpdate" :disabled="update_info.percent>0">立即更新</Button>
      <Button type="warning" :disabled="update_info.percent>0" @click="pauseUpdate">跳过版本</Button>
    </div>
  </div>

  <div class="update-info" v-if="hasCheckRs && route.name=='login'">
    <Alert type="warning" class="update-info-alert">{{ checkRs }}</Alert>
  </div>


</template>
<!--</template>-->

<script setup>
import {onMounted, ref, onUpdated} from 'vue'
import {storeToRefs} from "pinia";
import usePcUpdateStore from "module-base/views/update/store/update";
import {useRoute} from 'vue-router'

const route = useRoute()

let {showUpdate, version} = storeToRefs(usePcUpdateStore())
let {setShowUpdate} = usePcUpdateStore()

import {useUpdateBall} from "./updateBall";

const {
  update_info,
  particlesList,
  ballRadius,
  hasCheckRs,
  checkRs,
  setItemRef,
  createParticle,
  createAnimation
} = useUpdateBall()

import {useUpdateHandle} from './updateHandle'

const {
  pauseUpdate,
  startUpdate
} = useUpdateHandle(createAnimation, createParticle, version)

</script>

<style scoped>
.update-info, .progress-body {
  position: absolute;
  z-index: 9;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
}

.update-info {
  justify-content: end;
  z-index: 0;
}

.update-info :deep(.ivu-alert) {
  margin-bottom: 0;

}

.update-info-alert {
  justify-self: end;
  align-items: end;
  color: #ff9900;
  border-bottom-left-radius: 16px;
  border-bottom-right-radius: 16px;
}

.progress-body {
  position: absolute;
  z-index: 9;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: rgba(91, 98, 112, 0.9);
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  justify-content: center;
  align-content: center;
}

.ball {
  border-radius: 50%;
  //background: linear-gradient(135deg, #fff, #ddd);
  background-color: #fff;
  box-shadow: inset 0 0 0 2px rgba(0, 0, 0, .1),
  0 0 10px 2px rgba(0, 0, 0, .1);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
  flex-direction: column;
}

.ball::before {
  content: "";
  width: 80%;
  height: 80%;
  border-radius: 50%;
  //background: linear-gradient(315deg, #ddd, #fff);
  background-color: #fff;
  box-shadow: inset 0 0 0 2px rgba(0, 0, 0, .1),
  0 0 10px 2px rgba(0, 0, 0, .1);
  position: absolute;
}

.logo {
  max-width: 60%;
  max-height: 60%;
  z-index: 9;
  border-radius: 50%;
  width: 60px;
}

.particles {
  position: absolute;
  width: 100%;
  height: 100%;
}

.particle {
  //z-index: 9;
  width: 2px;
  height: 2px;
  border-radius: 50%;

  position: absolute;
}

:deep(.ivu-progress-show-info .ivu-progress-outer) {
  margin-right: 0;
  padding-right: 0;
}

:deep(.ivu-progress-text) {
  width: 100%;
}

.progress-action {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-content: center;
  margin-top: 16px;
}
.progress-extra{
  display: flex;
  justify-content: end;
}
.progress-extra .extra-close {
  float: right;
  color: #ed4014;
  cursor: pointer;
  border-radius: 50%;
  padding: 4px;
  margin-right: -50px;
}

.progress-extra .extra-close:hover {
  background-color: rgba(237, 64, 20,0.3);
}

</style>
